package com.seleniti.enpareja.android.ui.view;

import org.ioc.commons.impl.android.ui.HasEnabledWrapper;
import org.ioc.commons.impl.android.ui.HasVisibilityWrapper;
import org.ioc.commons.ui.HasValue;
import org.ioc.commons.ui.HasVisibility;

import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.seleniti.enpareja.android.R;
import com.seleniti.enpareja.android.ui.widget.PendingMark;
import com.seleniti.enpareja.controller.ui.view.MainView;
import com.seleniti.enpareja.controller.ui.view.MainView.Action;
import com.seleniti.enpareja.controller.ui.view.MainView.Event;
import com.seleniti.enpareja.controller.ui.view.MainView.Operation;
import com.seleniti.enpareja.controller.ui.view.MainView.Presenter;
import com.seleniti.enpareja.controller.ui.view.MainViewPresenter;

public class MainViewWidget extends EnParejaBaseView<Event, Operation, Action, Presenter> implements MainView {

	private Button btnShoppingList;
	private boolean enabled;
	private LinearLayout configUserSuggestionPanel;
	private Button btnHomeTasks;
	private Button btnMeals;
	private Button btnSharedExpenses;
	private Button btnWorkShifts;
	private Button btnGarage;
	private Button btnPendingAtHome;
	private PendingMark shoppingListPendingMark;
	private PendingMark homeTasksPendingMark;
	private PendingMark productsPendingMark;
	private View view;
	private Button btnGoSettings;
	private Button btnImportantDates;

	public MainViewWidget() {
		super(true);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		this.presenter = new MainViewPresenter(this, eventbus, operationManager, actionController);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		this.view = inflater.inflate(R.layout.main_view, container, false);

		this.btnShoppingList = (Button) view.findViewById(R.id.btnShoppingList);
		this.btnHomeTasks = (Button) view.findViewById(R.id.btnHomeTasks);
		this.btnMeals = (Button) view.findViewById(R.id.btnMeals);
		this.btnSharedExpenses = (Button) view.findViewById(R.id.btnSharedExpenses);
		this.btnWorkShifts = (Button) view.findViewById(R.id.btnWorkShifts);
		this.btnGarage = (Button) view.findViewById(R.id.btnGarage);
		this.btnPendingAtHome = (Button) view.findViewById(R.id.btnPendingAtHome);
		this.btnImportantDates = (Button) view.findViewById(R.id.btnImportantDates);
		this.shoppingListPendingMark = (PendingMark) view.findViewById(R.id.shoppingListPendingMark);
		this.homeTasksPendingMark = (PendingMark) view.findViewById(R.id.homeTasksPendingMark);
		this.productsPendingMark = (PendingMark) view.findViewById(R.id.productsPendingMark);
		this.btnGoSettings = (Button) view.findViewById(R.id.btnGoSettings);

		ViewCompat.setElevation(this.shoppingListPendingMark, 10.0f);
		ViewCompat.setElevation(this.homeTasksPendingMark, 11.0f);
		ViewCompat.setElevation(this.productsPendingMark, 12.0f);

		btnShoppingList.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				presenter.onGoToShoppingListView();
			}
		});
		btnHomeTasks.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				presenter.onGoToHomeTasksView();
			}
		});
		btnMeals.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				presenter.onGoToMealsView();
			}
		});
		btnSharedExpenses.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				presenter.onGoToSharedCostView();
			}
		});
		btnWorkShifts.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				presenter.onGoToWorkShiftsView();
			}
		});
		btnPendingAtHome.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				presenter.onGoToPendingShoppingView();
			}
		});

		this.btnGoSettings.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				presenter.onGoSettings();
			}
		});

		this.configUserSuggestionPanel = (LinearLayout) view.findViewById(R.id.configUserSuggestionPanel);

		return view;
	}

	@Override
	public boolean isEnabled() {
		return this.enabled;
	}

	@Override
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
		HasEnabledWrapper.setEnabled(enabled, btnShoppingList, btnHomeTasks, btnMeals, btnSharedExpenses,
				btnWorkShifts, btnGarage, btnPendingAtHome,btnImportantDates);

		// this.shoppingListPendingMark.bringToFront();
		// this.shoppingListPendingMark.invalidate();
		// if (this.shoppingListPendingMark.getParent() != null) {
		// this.shoppingListPendingMark.getParent().requestLayout();
		// if(this.shoppingListPendingMark.getParent() instanceof View) {
		// ((View)this.shoppingListPendingMark.getParent()).invalidate();
		// }
		// }

	}

	@Override
	public HasVisibility configUserSuggestionPanelVisibility() {
		return HasVisibilityWrapper.from(this.configUserSuggestionPanel, View.INVISIBLE, this);
	}

	@Override
	public HasValue<Integer> pendingShoppingList() {
		return this.shoppingListPendingMark;
	}

	@Override
	public HasValue<Integer> pendingHomeTasks() {
		return this.homeTasksPendingMark;
	}

	@Override
	public HasValue<Integer> pendingProducts() {
		return this.productsPendingMark;
	}

}
